package cn.mw.cmdb.timetask.controller;

import cn.mw.cmdb.timetask.entity.MwTimeTaskDto;
import cn.mw.cmdb.timetask.entity.MwTimeTaskTypeDto;
import cn.mw.cmdb.timetask.entity.TaskRecordDto;
import cn.mw.cmdb.timetask.param.MwTimeTaskParam;
import cn.mw.cmdb.timetask.service.MwTimeTaskService;
import cn.mw.microMonitorCommon.api.common.BaseApiService;
import cn.mw.microMonitorCommon.api.common.ResponseBase;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @description 定时任务
 */
@RequestMapping("/timeTask")
@Controller
@Tag(name = "定时任务管理")
@Slf4j
public class MwTimeTaskController extends BaseApiService {

    private MwTimeTaskService timeTaskService;

    public MwTimeTaskController(MwTimeTaskService timeTaskService) {
        this.timeTaskService = timeTaskService;
    }

    @PostMapping("/group/save")
    @ResponseBody
    @Operation(summary = "定时任务分组保存")
    public ResponseBase saveGroup(@RequestBody @Valid MwTimeTaskTypeDto timeTaskTypeDto) throws Exception {
        timeTaskService.saveTimeTaskType(timeTaskTypeDto);
        return setResultSuccess();
    }

    @PostMapping("/group/remove")
    @ResponseBody
    @Operation(summary = "定时任务分组删除")
    public ResponseBase removeGroup(@RequestBody MwTimeTaskParam timeTaskParam) throws Exception {
        timeTaskService.deleteTimeTaskType(timeTaskParam);
        return setResultSuccess();
    }


    @PostMapping("/group/list")
    @ResponseBody
    @Operation(summary = "定时任务分组查询")
    public ResponseBase selectGroup(@RequestBody MwTimeTaskParam timeTaskParam) throws Exception {
        List<MwTimeTaskTypeDto> taskTypeDtos = timeTaskService.selectTimeTaskTypeList(timeTaskParam);
        return setResultSuccess(taskTypeDtos);
    }


    @PostMapping("/list")
    @ResponseBody
    @Operation(summary = "定时任务查询")
    public ResponseBase selectTimeTask(@RequestBody MwTimeTaskParam timeTaskParam) throws Exception {
        Page page = timeTaskService.selectTimeTask(timeTaskParam);
        return setResultSuccess(page);
    }

    @PostMapping("/save")
    @ResponseBody
    @Operation(summary = "定时任务保存")
    public ResponseBase saveTimeTask(@RequestBody MwTimeTaskDto timeTaskDto) throws Exception {
        timeTaskService.save(timeTaskDto);
        return setResultSuccess();
    }

    @PostMapping("/remove")
    @ResponseBody
    @Operation(summary = "定时任务删除")
    public ResponseBase removeTimeTask(@RequestBody MwTimeTaskParam timeTaskParam) throws Exception {
        timeTaskService.remove(timeTaskParam);
        return setResultSuccess();
    }

    @PostMapping("/getTaskExecuteRecord")
    @ResponseBody
    @Operation(summary = "查询任务执行记录")
    public ResponseBase getTaskExecuteRecord(@RequestBody MwTimeTaskParam timeTaskParam) throws Exception {
        Page page = timeTaskService.getTaskExecuteRecord(timeTaskParam);
        return setResultSuccess(page);
    }

    @PostMapping("/getTaskInstanceDetails")
    @ResponseBody
    @Operation(summary = "查询任务实例详情")
    public ResponseBase getTaskInstanceDetails(@RequestBody MwTimeTaskParam timeTaskParam) throws Exception {
        TaskRecordDto taskRecordDto = timeTaskService.getTaskInstanceDetails(timeTaskParam);
        return setResultSuccess(taskRecordDto);
    }

    @PostMapping("/getTaskLog")
    @ResponseBody
    @Operation(summary = "获取任务日志")
    public ResponseBase getTaskLog(@RequestBody MwTimeTaskParam timeTaskParam) throws Exception {
        TaskRecordDto taskRecordDto = timeTaskService.getTaskRunLog(timeTaskParam);
        return setResultSuccess(taskRecordDto);
    }

    @PostMapping("/runTask")
    @ResponseBody
    @Operation(summary = "任务执行")
    public ResponseBase runTask(@RequestBody MwTimeTaskParam timeTaskParam) throws Exception {
        timeTaskService.runTask(timeTaskParam);
        return setResultSuccess();
    }

    @PostMapping("/updateTaskStatus")
    @ResponseBody
    @Operation(summary = "修改任务状态")
    public ResponseBase updateTaskStatus(@RequestBody MwTimeTaskParam timeTaskParam) throws Exception {
        timeTaskService.updateTaskStatus(timeTaskParam);
        return setResultSuccess();
    }

    @PostMapping("/retry")
    @ResponseBody
    @Operation(summary = "任务重试")
    public ResponseBase taskRetry(@RequestBody MwTimeTaskParam timeTaskParam) throws Exception {
        timeTaskService.taskRetry(timeTaskParam);
        return setResultSuccess();
    }

    @PostMapping("/stop")
    @ResponseBody
    @Operation(summary = "任务停止")
    public ResponseBase taskStop(@RequestBody MwTimeTaskParam timeTaskParam) throws Exception {
        timeTaskService.taskStop(timeTaskParam);
        return setResultSuccess();
    }


    @PostMapping("/checkTimeExpression")
    @ResponseBody
    @Operation(summary = "校验定时器表达式")
    public ResponseBase checkTimeExpression(@RequestBody MwTimeTaskParam timeTaskParam) throws Exception {
        List list = timeTaskService.checkTimeExpression(timeTaskParam);
        return setResultSuccess(list);
    }


}
